package cn.qhfn.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 数据库操作工具类
 */
public class BaseDao {
    private static String driver = null; //驱动名
    private static String url = null; //连接字符串
    private static String username = null; //DB用户名
    private static String password = null; //DB密码
    public Connection conn = null; //数据库连接
    public PreparedStatement pstmt = null; //发送sql
    public ResultSet rs = null; //结果集
    static Properties properties = new Properties(); //读取配置文件

    /**
     * 获取数据库配置
     */
    static {
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
        try {
            properties.load(is);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接方法
     * @return
     */
    public Connection getConn(){
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url,username,password);
        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 释放资源方法
     * @param conn
     * @param pstmt
     * @param rs
     */
    public void closeAll(Connection conn,PreparedStatement pstmt,ResultSet rs){
        try {
            if(conn != null){
                conn.close();
            }
            if(pstmt != null){
                pstmt.close();
            }
            if(rs != null){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增删改的方法
     * @param sql
     * @param params
     * @return
     */
    public int executeUpdate(String sql,Object[] params){
        int count = 0;
        conn = getConn();
        try {
            pstmt = conn.prepareStatement(sql);
            if(params.length != 0){
                for (int i = 0;i<params.length;i++){
                    pstmt.setObject(i+1,params[i]);
                }
            }
            count = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,null);
        }
        return count;
    }

    /**
     * 查询方法
     * @param sql
     * @param params
     * @return
     */
    public ResultSet executeQuery(String sql,Object... params){
        conn = getConn();
        try {
            pstmt = conn.prepareStatement(sql);
            if(params.length != 0){
                for (int i = 0;i<params.length;i++){
                    pstmt.setObject(i+1,params[i]);
                }
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
}